/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.client.chooser.view;
/*
 By  RepositoryNode.java
 Created on 14. September 2006, 13:53
 */

import java.util.Iterator;
import javax.swing.tree.DefaultMutableTreeNode;
import offset.nodes.client.chooser.model.FolderNode;
import offset.nodes.client.chooser.model.FolderPopulator;

/**
 *
 * @author Walter Lütgenau
 */
public class LazyExpandingNode  extends DefaultMutableTreeNode {
    FolderPopulator populator;
    private String path;
    FolderNode node;
    
    public LazyExpandingNode(FolderPopulator populator, FolderNode node) {
        this.populator = populator;
        this.node = node;
        setUserObject(node);
    }
    
// Override isLeaf to check whether this is a directory
    public boolean isLeaf() {
        return !node.hasChildren();
    }
    
// Override getAllowsChildren to check whether
// this is a directory
    public boolean getAllowsChildren() {
        return node.hasChildren();
    }
    
    protected String getLastPathComponent( String path) {
        int index = path.lastIndexOf("/");
        if ( index < 0)
            return path;
        return path.substring(index+1);
    }
    
    /**
     * Populate the children of the node
     * 
     * @return
     * @throws Exception
     */
    public boolean populateChildren() throws Exception {
         // Do this only once
        if (populated == false) {
            Iterator i = populator.populate(node.getPath()).iterator();
            
            // Process the directories
            while (i.hasNext()) {
                FolderNode child = (FolderNode) i.next();
                
                this.add(new LazyExpandingNode(populator, child));
             }
            
             populated = true;
           }
        
        return getChildCount() > 0;
    }
    
    public void repopulateFolders() throws Exception{
        populated = false;
        populateChildren();
    }
    
    
    
    protected boolean populated; // true if we have been populated
 
    public String getName() {
        return getLastPathComponent(node.getPath());
    }

     public FolderNode getNode() {
        return node;
    }

    public void setNode(FolderNode node) {
        this.node = node;
    }

    public void setPopulated(boolean populated) {
        this.populated = populated;
    }
}
